<?php
require_once DATA_ROOT . 'BaseData.php';
/**
 * This is the model class for table "{{user_relation}}".
 *
 * The followings are the available columns in table '{{user_relation}}':
 * @property string $uid1
 * @property string $uid2
 * @property string $create_time
 * @property string $last_modify_time
 * @property string $status
 */
class UserRelationData extends BaseData
{
    /**
     * Returns the static model of the specified AR class.
     * @return UserRelationDao the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{user_relation}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('uid1, uid2, create_time, last_modify_time', 'required'),
            array('uid1, uid2, create_time, last_modify_time', 'length', 'max'=>10),
            array('status', 'length', 'max'=>5),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('uid1, uid2, create_time, last_modify_time, status', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'uid1' => 'Uid1',
            'uid2' => 'Uid2',
            'create_time' => 'Create Time',
            'last_modify_time' => 'Last Modify Time',
            'status' => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('uid1',$this->uid1,true);
        $criteria->compare('uid2',$this->uid2,true);
        $criteria->compare('create_time',$this->create_time,true);
        $criteria->compare('last_modify_time',$this->last_modify_time,true);
        $criteria->compare('status',$this->status,true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria'=>$criteria,
        ));
    }
    
    public function updateStatus($status) {
        $this->status = $status;
        $this->last_modify_time = time();
        $this->setIsNewRecord(false);
        $res = $this->update(array('status', 'last_modify_time'));
        if (false === $res) {
            throw new Exception('error in updating status for user relation uid1[' . $this->uid1 . '], uid2[' . $this->uid2 . ']', ErrorDef::COMMON_ERROR_SYS);
        }
    }
    
    public static function genUserRelation($uid_follow, $uid_followed) {
        $userRelation = new UserRelationData();
        $userRelation->uid1 = $uid_follow;
        $userRelation->uid2 = $uid_followed;
        $userRelation->last_modify_time = time();
        $userRelation->status = UserRelationStatus::NORMAL;
        $res = $userRelation->insert();
        if (false === $res) {
            throw new Exception('error in insert user relation uid1[' . $userRelation->uid1 . '], uid2[' . $userRelation->uid2 . ']', ErrorDef::COMMON_ERROR_SYS);
        }
    }

}
